<?php

class ResourceService extends BaseService {
   
	public function getResourceById($resId) {
        $resource = Resource::model()->findByPk($resId);

        return $this->toArray($resource);
    }    
    
    public function getResources($conditions, $with = null) {
        if ($conditions instanceof CDbCriteria) {
            $criteria = $conditions;
        } 
        else {
            $criteria = $this->toCriteria($conditions);
        }

        try{
            if($with != null) {
                $resources = Resource::model()->with($with)->findAll($criteria);
                return $this->toArray($resources, $with);
            } 
            else {
                $resources = Resource::model()->findAll($criteria);
                return $this->toArray($resources); 
            }
        }
        catch(Exception $e) {
            Yii::log('get resources failed:'. $e->getMessage(), CLogger::LEVEL_ERROR);
            return false;
        }
    }   
    
    public function getPartitions($physicalId) {
    	$partitions = ResourcePartition::model()->findAll(array(
			'select'	=>	'physical_id, partition',
			'condition'	=>	'physical_id=:physical_id',
			'param'		=>	array(
				':physical_id'	=>	$physicalId,
    		)
    	));
    	return $this->toArray($partitions);
    }

    public function getResourcePhysicalById($resId) {
        $res_ph = ResourcePhysical::model()->findAll(
        	array(
        		'condition'	=>	'res_id=:res_id',
        		'params'	=>	array(
        			':res_id'	=>	$resId,
        		),
        		'order'	=>	'physical_type DESC',
        	)
        );

        return $this->toArray($res_ph);
    }  

    public function getResourcePhysicalByType($resId, $physicalType) {
        $res_ph = ResourcePhysical::model()->find(
        	array(
        		'condition'	=>	'res_id=:resId and physical_type=:physicalType',
        		'params'	=>	array(
        			':resId'		=>	$resId,
        			':physicalType'	=>	$physicalType,
        		),
        	)
        );

        return $this->toArray($res_ph);
    }  

    public function editResourceAttributes($resourceId, $attributes) {
    	return Resource::model()->updateByPk($resourceId, $attributes);
    }
    
    public function addOutputResource($resource) {
    	try {			
			$tmpResource = Resource::model()->findByAttributes(array(
    			'res_name'	=>	$resource['res_name'],
    		));
			if(!isset($tmpResource)) {
				$outputResource = new Resource;
				$outputResource->setAttributes($resource, false);
				$status = $outputResource->save();
		
				return $status == true ? $outputResource->res_id : $status;
			}
			else {
				return $tmpResource->res_id;
			}	
		}
		catch(Exception $e) {
			Yii::log("add output resource error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
    }
	
	public function addResourcePhysical($resourcePhysical) {
		try {
			$allPhysicals = ResourcePhysical::model()->findAllByAttributes(array(
				'res_id'		=>	$resourcePhysical['res_id'],
				'physical_type'	=>	$resourcePhysical['physical_type'],
				)
			);
			if(count($allPhysicals) == 0) {
				$resPhysical = new ResourcePhysical;
				$resPhysical->setAttributes($resourcePhysical, false);
				return $resPhysical->save() ? $resPhysical->physical_id : false; 
			}
			else {
				return $allPhysicals[0]['physical_id'];
			}
		}
		catch (Exception $e) {
			Yii::log("add physical resource error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function addResourcePartition($partition) {
		try {
			$allPartitions = ResourcePartition::model()->findAllByAttributes(array(
				'physical_id'	=>	$partition['physical_id'],
				'partition'		=>	$partition['partition'],
				)
			);
			if(count($allPartitions) == 0) {
				$newPartition = new ResourcePartition;
				$newPartition->setAttributes($partition, false);
				$status = $newPartition->save();
			}
			else {
				ResourcePartition::model()->updateByPk($allPartitions[0]->partition_id, $partition);
				return $allPartitions[0]->partition_id;
			}
	
			return $status == true ? $newPartition->partition_id : $status;
		}
		catch(Exception $e) {
			Yii::log("add resource partition error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function getStorageSystem($systemId) {
		$storageSystem = StorageSystem::model()->findByPk($systemId);
        return $this->toArray($storageSystem);
	}
	
	public function getStorageSystemByType($physicalType) {
		$storageSystem = StorageSystem::model()->findByAttributes(array('storage_type'=>$physicalType));
        return $this->toArray($storageSystem);
	}
	
	public function checkResourcePartition($physicalId, $partition) {
		$partition = ResourcePartition::model()->find(array(
			'condition'	=>	'physical_id=:physicalId and partition=:partition',
			'params'		=>	array(
				':physicalId'	=>	$physicalId,
				':partition'	=>	$partition,
				)
			)
		);
		return isset($partition);
	}

	public function getResourcePartition($physicalId, $partition) {
		$partition = ResourcePartition::model()->find(array(
			'condition'	=>	'physical_id=:physicalId and partition=:partition',
			'params'		=>	array(
				':physicalId'	=>	$physicalId,
				':partition'	=>	$partition,
				)
			)
		);
		return $this->toArray($partition);
	}
	
}
